﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.ComponentModel;
using AjaxControlToolkit;

public partial class Admin_UserControl_QuanLyQuangCao : System.Web.UI.UserControl
{


    
   
    protected void Page_Load(object sender, EventArgs e)
    {
       if( !IsPostBack)
       {
           
       }
    }

    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }
    protected void DetailsView1_PageIndexChanging1(object sender, DetailsViewPageEventArgs e)
    {
        
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        pnlThemLoaiQuangCao.Visible = true;
        pnlChiTietLoaiQuangCao.Visible = false;
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        pnlThemLoaiQuangCao.Visible = false;
        pnlChiTietLoaiQuangCao.Visible = true;
    }
    protected void DetailsView2_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
       // Response.Write("<script> alert('Trả lời thành công');</script>");
        Response.Redirect(@"~/Admin/QuanlyQuangCao.aspx");
    }
    protected void btnThem_Click1(object sender, EventArgs e)
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        VI_TRI_QUANG_CAO vitri = new VI_TRI_QUANG_CAO();
        vitri.TenLoaiViTri = txtTenViTri.Text;

        try
        {
            vitri.Width = int.Parse(txtWidth.Text);
            vitri.Heigth = int.Parse(txtHeight.Text);

            vitri.ThoiGianLuanChuyen = int.Parse(txtThoiGianLuanChuyen.Text);

            vitri.TienQuangCao = long.Parse(txtTienQuangCao.Text);

        }
        catch (System.Exception ex)
        {
        	return;
        }

        vitri.Deleted = ckDeleted.Checked;
        
        linQ.VI_TRI_QUANG_CAOs.InsertOnSubmit(vitri);
        linQ.SubmitChanges();

        Response.Redirect(@"~/Admin/QuanLyQuangCao.aspx");
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btnEdit = (Button)sender;

        int maQuangCao = int.Parse(btnEdit.ToolTip);

        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        var qc = linQ.QUANG_CAOs.Where(quangcao => quangcao.MaQuangCao == maQuangCao);

        if( qc.Count() >= 1)
        {
            pnlEdit.Visible = true;
            DetailsView3.DataSource = qc;
            DetailsView3.DataBind();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        DropDownList dropKH = (DropDownList)DetailsView3.FindControl("DropDownList3");

        DropDownList dropLoaiQc = (DropDownList)DetailsView3.FindControl("DropDownList2");

        Label maQuangCao = (Label)DetailsView3.FindControl("Label1");
        TextBox txtTenQuangCao = (TextBox)DetailsView3.FindControl("TextBox1");

        TextBox txtLink = (TextBox)DetailsView3.FindControl("TextBox2");

        TextBox txtThoiGianDang = (TextBox)DetailsView3.FindControl("TextBox3");

        TextBox txtThoiHetHan = (TextBox)DetailsView3.FindControl("TextBox4");

        TextBox txtThoiNoiDung = (TextBox)DetailsView3.FindControl("TextBox5");

        TextBox txtThoiGianAnhDong = (TextBox)DetailsView3.FindControl("TextBox6");

        TextBox txtImage = (TextBox)DetailsView3.FindControl("TextBox7");

        TextBox txtHeight = (TextBox)DetailsView3.FindControl("TextBox9");

        TextBox txtWidth = (TextBox)DetailsView3.FindControl("TextBox8");

        CheckBox ckDeleted = (CheckBox)DetailsView3.FindControl("CheckBox1");

        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        QUANG_CAO qc = linQ.QUANG_CAOs.Single(quangcao => quangcao.MaQuangCao == int.Parse(maQuangCao.Text));

        qc.MaKhachHang = int.Parse(dropKH.SelectedValue);
        qc.MaLoaiQuangCao = int.Parse(dropLoaiQc.SelectedValue);

        qc.TenQuangCao = txtTenQuangCao.Text;
      /*  CalendarExtender a = (CalendarExtender)DetailsView3.FindControl("CalendarExtender1");*/

     //   qc.ThoiGianDang;
       qc.ThoiGianDang = DateTime.Parse(txtThoiGianDang.Text);
        qc.ThoiGianHetHan = DateTime.Parse(txtThoiHetHan.Text);

        qc.NavigateUrl = txtLink.Text;
        qc.NoiDungQuangCao = txtThoiNoiDung.Text;
        qc.ThoiGianAnhDong = int.Parse(txtThoiGianAnhDong.Text);
        qc.ImageUrl = txtImage.Text;
        qc.Height = int.Parse(txtHeight.Text);
        qc.Width = int.Parse(txtWidth.Text);
        qc.Deleted = ckDeleted.Checked;

        linQ.SubmitChanges();

        Response.Write("<script> alert('Thay đổi quảng cáo thành công');</script>");
        GridView1.DataBind();

    }
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        DropDownList dropKH = (DropDownList)DetailsView2.FindControl("DropDownList3");

        DropDownList dropLoaiQc = (DropDownList)DetailsView2.FindControl("DropDownList2");

        TextBox txtTenQuangCao = (TextBox)DetailsView2.FindControl("TextBox1");

        TextBox txtLink = (TextBox)DetailsView2.FindControl("TextBox2");

        TextBox txtThoiGianDang = (TextBox)DetailsView2.FindControl("TextBox3");

        TextBox txtThoiHetHan = (TextBox)DetailsView2.FindControl("TextBox4");

        TextBox txtThoiNoiDung = (TextBox)DetailsView2.FindControl("TextBox5");

        TextBox txtThoiGianAnhDong = (TextBox)DetailsView2.FindControl("TextBox6");

        TextBox txtImage = (TextBox)DetailsView2.FindControl("TextBox7");

        TextBox txtHeight = (TextBox)DetailsView2.FindControl("TextBox9");

        TextBox txtWidth = (TextBox)DetailsView2.FindControl("TextBox8");

        CheckBox ckDeleted = (CheckBox)DetailsView2.FindControl("CheckBox1");

        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        QUANG_CAO qc = new QUANG_CAO();

        qc.MaKhachHang = int.Parse(dropKH.SelectedValue);
        qc.MaLoaiQuangCao = int.Parse(dropLoaiQc.SelectedValue);

        qc.TenQuangCao = txtTenQuangCao.Text;
        /*  CalendarExtender a = (CalendarExtender)DetailsView3.FindControl("CalendarExtender1");*/

        //   qc.ThoiGianDang;
        qc.ThoiGianDang = DateTime.Parse(txtThoiGianDang.Text);
        qc.ThoiGianHetHan = DateTime.Parse(txtThoiHetHan.Text);

        qc.NavigateUrl = txtLink.Text;
        qc.NoiDungQuangCao = txtThoiNoiDung.Text;
        qc.ThoiGianAnhDong = int.Parse(txtThoiGianAnhDong.Text);
        qc.ImageUrl = txtImage.Text;
        qc.Height = int.Parse(txtHeight.Text);
        qc.Width = int.Parse(txtWidth.Text);
        qc.Deleted = ckDeleted.Checked;

        qc.MaViTri = int.Parse(DropDownList1.SelectedValue);
        linQ.QUANG_CAOs.InsertOnSubmit(qc);
        linQ.SubmitChanges();

        Response.Write("<script> alert('Thêm quảng cáo thành công');</script>");
        GridView1.DataBind();


    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        
        VI_TRI_QUANG_CAO viTri = linQ.VI_TRI_QUANG_CAOs.Single(vt => vt.MaViTri == int.Parse(DropDownList1.SelectedValue));

        linQ.VI_TRI_QUANG_CAOs.DeleteOnSubmit(viTri);

        Response.Write("<script> alert('Xóa vị trí quảng cáo thành công');</script>");

        DropDownList1.DataBind();

    }
    protected void btnAn_Click(object sender, EventArgs e)
    {
        pnlThemLoaiQuangCao.Visible = false;
        pnlChiTietLoaiQuangCao.Visible = false;
    }
    protected void btnXoa_Click1(object sender, EventArgs e)
    {
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
     
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Label maQuangCao = (Label)DetailsView3.FindControl("Label1");

        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        QUANG_CAO qc = linQ.QUANG_CAOs.Single(quangcao => quangcao.MaQuangCao == int.Parse(maQuangCao.Text));

        linQ.QUANG_CAOs.DeleteOnSubmit(qc);

        linQ.SubmitChanges();

        Response.Write("<script> alert('Xóa  quảng cáo thành công');</script>");

        GridView1.DataBind();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        pnlInsertQuangCao.Visible = false;
        pnlInsertQuangCaoButton.Visible = true;
    }
    protected void btnThemQuangCao_Click(object sender, EventArgs e)
    {
        pnlInsertQuangCao.Visible = true;
        pnlInsertQuangCaoButton.Visible = false;
    }
    protected void DetailsView2_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }
    protected void DetailsView3_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        pnlEdit.Visible = false;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
}
